package com.platform.task.job;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.platform.task.constant.Constant;
import com.platform.task.service.BaseCyberService;
import com.platform.task.service.impl.MsmqDataServiceHandler;

@Component
public class ReadDataFromMsmq implements InitializingBean {
	private static final Log log = LogFactory.getLog(ReadDataFromMsmq.class);
	@Autowired
	MsmqDataServiceHandler msmqDataServiceHandler;

	@Autowired
	public BaseCyberService baseCyberService;

	// 距当前时间多长的时间后，系统认定为历史数据，给予抛弃处理；默认为毫秒
	// @Value("${validateTime}")
	public String validateTime = "30000";

	// @Value("${KafkaDataAnalysisType}")
	public String type = "1";

	public void afterPropertiesSet() {
		ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);
		scheduledThreadPool.schedule(new Runnable() {
			@Override
			public void run() {
				try {
					 Constant.baseCyberService = baseCyberService;
					Receive();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, 10, TimeUnit.SECONDS);
	}

	/**
	 * 多线程执行service业务处理，service為多实例模式
	 */
	public void Receive() throws java.io.UnsupportedEncodingException {
		ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
		for (int i = 0; i < Constant.thread_Num; i++) {
			fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					while (true) {
						try {
							log.info("task is start............");
							// 业务处理
							 msmqDataServiceHandler.handlerByAvro("", "");

							log.info("task is end............");
						} catch (Exception e) {
							log.error("task has exception", e);
							// 如果是 网络导致 断掉，则重新打开
						}
					}
				}
			});
		}
	}

}
